package demo

import (
	"encoding/json"
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/golang/protobuf/proto"
	"io/ioutil"
	"wx-api/adx-bid/models/bid/BidModel"
	"wx-api/adx-bid/utils"
)

func BidDemo(c *gin.Context) {
	cty := c.GetHeader("Content-Type")
	body, err := ioutil.ReadAll(c.Request.Body)
	if err != nil {
		fmt.Println("BidDemo read body err:", err.Error())
		c.JSON(200, gin.H{
			"code": 1,
			"msg":  "BidDemo read body err:" + err.Error(),
			"data": nil,
		})
		return
	}
	ret := &BidModel.BidRequest{}
	if cty != "application/json" {
		err = proto.Unmarshal(body, ret)
	} else {
		err = json.Unmarshal(body, ret)
	}
	if err != nil {
		fmt.Println("BidDemo unmarshal err:", err.Error())
	}
	res := &BidModel.BidResponse{
		Id:    ret.GetId(),
		Bidid: utils.GetUUid(),
		Tms:   50,
		Seatbid: []*BidModel.BidResponse_SeatBid{
			&BidModel.BidResponse_SeatBid{
				Bid: []*BidModel.BidResponse_SeatBid_Bid{
					&BidModel.BidResponse_SeatBid_Bid{
						Id:             ret.GetId(),
						Impid:          "1",
						Price:          float32(utils.RandInt64(0, 300)) + float32(utils.RandFloat()),
						Adid:           "1111",
						Cid:            "1111",
						W:              360,
						H:              480,
						Title:          "demo ad",
						Desc:           "demo ad desc",
						ClickUrl:       "http://wwww.baidu.com",
						Dpurl:          "deeplink_url",
						AdType:         1,
						Views:          []string{"http://www.pv1.com"},
						Clicks:         []string{"http://www.ck1.com"},
						DplinksSuccess: []string{"http://www.sdp1.com"},
						Clicktype:      "2",
						App: &BidModel.BidResponse_SeatBid_Bid_App{
							Name: "app name",
							Pkg:  "app pkg",
							Icon: "app icon",
							Size: 10000,
							Ver:  "1.2.9",
							Dev:  "huayan",
							Pri:  "app pri",
							Per:  "app per",
							Furl: "app function url",
							Fstr: "app function str",
						},
						Imgs: []*BidModel.BidResponse_SeatBid_Bid_Image{
							&BidModel.BidResponse_SeatBid_Bid_Image{
								W:   360,
								H:   480,
								Url: "https://img2.baidu.com/it/u=495542229,3606133816&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=749",
							},
						},
					},
				},
			},
		},
	}
	if cty != "application/json" {
		c.ProtoBuf(200, res)
	} else {
		c.JSON(200, res)
	}
	return
}
